# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
import time,json,requests

demoinit=open("../demo.json","r")
demoinit=demoinit.read()
demoinit=json.loads(demoinit)
url=demoinit["site"]+"/api/getdemo.php"
demo=requests.get(url)
demo=json.loads(demo.text)
doub=requests.get(demo["site"]+"/api/run.php")
doub=json.loads(doub.text)
driver = webdriver.Firefox()

def head():
	driver.get('https://www.5199.vip/home/')
	time.sleep(100)
	pk=0 
	while True:
		try:
			es=""
			es=driver.find_element(By.CSS_SELECTOR,".sc-1f30fun-2.eQMIGV .sc-1f30fun-4.jowkKN")
			es=es.text
			es=es.encode("utf-8")
			es=es.strip()
		except Exception as e:
			print("error status 1")

		else:	
			if es != "已封盘" :
				while True:
					try:
						es=""
						es=driver.find_element(By.CSS_SELECTOR,".sc-1f30fun-2.eQMIGV .sc-1f30fun-4.jowkKN")
						es=es.text
						es=es.encode("utf-8")
						es=es.strip()
					except Exception as e:
						print("error status 2")	
					else:
						if es =="已封盘":
							while True:
								try:
									es=""
									es=driver.find_element(By.CSS_SELECTOR,".sc-1f30fun-2.eQMIGV .sc-1f30fun-4.jowkKN")
									es=es.text
									es=es.encode("utf-8")
									es=es.strip()
								except Exception as e:
									print("error status 3")
								else:
									if es != "已封盘":
										pk=1
										break
					if pk==1:
						break
		if pk==1:
			break
	print("starting...")
def make():
	tree={}
	tree=requests.get(demo["site"]+"/api/gettree.php")
	tree=json.loads(tree.text)
	for a in range(0,len(tree)):
		for b in range(0,len(tree[a]["tree"])):
			if len(tree[a]["tree"][b]["order"])>0:
				for c in range(0,len(tree[a]["tree"][b]["order"])):
					dom=""
					money=tree[a]["tree"][b]["order"][c]["money"]
					moneys=str(money)
					dom=tree[a]["trackdom"]+tree[a]["tree"][b]["projectdom"]
					try:
						element=driver.find_element(By.CSS_SELECTOR,dom)
						element.click()
						elemen=driver.find_element(By.CSS_SELECTOR,dom)
						for x in moneys:
							elemen.send_keys(x)
						print(moneys)
					except Exception as e:
						print("error!!!")
					
			else :
				continue
	try:
		el=driver.find_element(By.CSS_SELECTOR,".sc-2bhkbz-0.fBmSuo .sc-2bhkbz-1.kVjCHR .sc-1iw0f4-0.gmwSkA.sc-heq5nv-0.sc-2bhkbz-6.hAIctb.fjBrls")
		el.click()
	except Exception as e:
		print("error!!!")
	time.sleep(1)
	try:
		els=driver.find_element(By.CSS_SELECTOR,".sc-uoa0md-1.gYfFKC .sc-1iw0f4-0.gmwSkA.sc-heq5nv-0.sc-19tubnh-0.hAIctb.kfaAmq")
		els.click()
	except Exception as e:
		print("error!!!")
	time.sleep(1)

def sel():
	i=0
	while True:
		status=0
		while True:
			try:
				es=""
				es=driver.find_element(By.CSS_SELECTOR,".sc-1f30fun-2.eQMIGV .sc-1f30fun-4.jowkKN")
				es=es.text
			except Exception as e:
				if status==5:
					driver.refresh()
					break
				status=status+1
				print("获取当前状态失败,正在重新执行中...")
			else:
				break
		es=es.encode("utf-8")
		es=es.strip()
		if es != "已封盘":
			if i<1:
				i=i+1
			elif i<3 and i==2:
				i=i+1
		elif es == "已封盘":
			if i<2 and i==1:
				i=i+1
		if i==3:
			break
	time.sleep(1)
	print("starting...")
	url=demo["site"]+"/api/copyorder.php"
	res=requests.get(url)
	
def pk(x):
	print(x)
	if x > 1:
		while True:
			qh=""
			jm=""
			qh=driver.find_element(By.CSS_SELECTOR,".sc-1ixr2qq-1.eiwGGm .sc-1ixr2qq-3.kjvYbP")
			jm=driver.find_element(By.CSS_SELECTOR,".sc-l0mymw-1.kNHxYF").get_attribute('outerHTML')
			d={"qh":qh.text.encode("utf-8"),"jm":jm}
			url=demo["site"]+"/api/pkresult.php?qh="+qh.text+"&jm="+jm
			res=requests.post(url=url,data=d)
			restext=json.loads(res.text)
			print(restext)
			if restext["code"]==200:
				res=requests.post(url=url,data=d)
				break
		time.sleep(1)

def run():
	prev={}
	prev=requests.get(demo["site"]+"/api/prev.php")
	prev=json.loads(prev.text)
	pid=""
	money=0
	treeid=""
	issue=""
	url=demo["site"]+"/api/pid.php"
	pid=requests.get(url)
	pid=json.loads(pid.text)
	pid=pid["pid"]

	for x in range(0,len(prev)):
		while True:
			try:
				is_sue=driver.find_element(By.CSS_SELECTOR,".sc-1f30fun-2.eQMIGV .sc-1f30fun-3.eXhGNg")
				is_sue=is_sue.text
			except Exception as e:
				print("获取当前期号失败,正在重新执行中...")
			else:
				break
		is_sue=is_sue.encode("utf-8")
		issue=is_sue.strip()
		# prev[x]["result"]="no"
		ispoin=prev[x]["poin"]
		if prev[x]["result"]=="no":
			treeid=prev[x]["tree_id"]
			if pid==0:
				pid=prev[x]["pid"]
			if ispoin+1==len(doub):
				money=doub[0]
				ispoined=0
			else:
				money=doub[ispoin+1]
				ispoined=ispoin+1
			victory(prev[x]["money"],"no")
		else:
			treeid=prev[x]["tree_id"]
			if pid==0:
				pid=prev[x]["pid"]
			money=doub[0]
			ispoined=0
			victory(prev[x]["money"],"yes")
		
		url=""
		url=demo["site"]+"/api/order.php?issue="+issue+"&treeid="+str(treeid)+"&pid="+str(pid)+"&money="+str(money)+"&poin="+str(ispoined)+"&id="+str(prev[x]["id"])
		requests.get(url)
	url=demo["site"]+"/api/auto.php"
	res=requests.get(url)

def victory(money,is_yes):
	url=demo["site"]+"/api/victory.php?victory="+str(money)+"&is_yes="+is_yes
	info=requests.get(url)
	info=json.loads(info.text)
	if info["msg"]=="stop" and info["count"]==10:
		print("已到达预停状态...")
		time.sleep(1)
		print(3)
		time.sleep(1)
		print(2)
		time.sleep(1)
		print(1)
		time.sleep(1)
		print(0)
		exit()
		
if __name__ == '__main__':
	head()
	x=0
	while True:
		make()
		sel()
		pk(x)
		run()
		x=x+1